package cn.tedu.project.oa.asset.controller;

import cn.tedu.project.oa.asset.mapper.AssetMapper;
import cn.tedu.project.oa.asset.pojo.dto.AssetQuery;
import cn.tedu.project.oa.asset.pojo.dto.AssetSaveParam;
import cn.tedu.project.oa.asset.pojo.vo.AssetVO;
import cn.tedu.project.oa.asset.service.AssetService;
import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.response.PageData;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/oa/asset")
@Api(tags = "04.固定资产管理+盘点")
public class AssetController {

    @Autowired
    AssetService assetService;

    @GetMapping("select")
    @ApiOperation("查询资产列表")
    public JsonResult selectAsset(AssetQuery assetQuery){
        log.debug("查询固定资产列表:assetQuery={}",assetQuery);

        PageData data = assetService.selectAsset(assetQuery);

//        List<AssetVO> list = assetService.selectAsset(assetQuery);

        return JsonResult.ok(data);
    }

    @PostMapping("save")
    @ApiOperation("新增+编辑资产")
    public JsonResult saveAsset(@Validated AssetSaveParam assetSaveParam){
        log.debug("保存固定资产：assetSaveParam={}",assetSaveParam);

        assetService.saveAsset(assetSaveParam);

        return JsonResult.ok();
    }

    @PostMapping("delete/{id}/{code}")
    @ApiOperation("删除资产")
    public JsonResult deleteAsset(@PathVariable Long id,@PathVariable String code){
        log.debug("删除固定资产:id={},assetCode={}",id,code);

        assetService.deleteAsset(id,code);

        return JsonResult.ok();
    }

    @PostMapping("confirm/{id}/{checkNum}")
    @ApiOperation("审批盘点")
    public JsonResult auditCheck(@PathVariable Long id,@PathVariable Long checkNum){
        log.debug("更新盘点数:id={},checkNum={}",id,checkNum);

        assetService.updateStockNum(id,checkNum);

        return JsonResult.ok();
    }

    @GetMapping("count")
    @ApiOperation("展示固定资产数量")
    public JsonResult assetCount(){
        log.debug("展示目前资产数量");

        int num = assetService.count();

        return JsonResult.ok(num);
    }
    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "3";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }
}
